package objects;

import graphics.MaxGraphics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D.Double;

import animation.Explosion;

import main.Game;

import structure.SObject;
import util.LinAlg;
import util.Point;
import weapons.Shot;

public class AsteroidForceField extends SObject {

	private int radius, xRad, yRad;
	private static final int MARGIN = 70;
	private double health = 1;
	
	public AsteroidForceField(Point p, int radius) {
		super(p);
		this.radius = radius;
		this.xRad = (int) 10;
		this.yRad = (int) 10;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw(MaxGraphics g) {
		g.setColor(Color.white);
		g.drawOval(getX(), getY(), xRad, yRad);
		g.setColor(Color.gray);
		g.fillRect(getX(), getY(), 10, 10);
		g.setColor(new Color(50, 50, 200));
	}

	@Override
	public void collide(SObject s) {

		if(s instanceof Asteroid) {
			
			Ellipse2D.Double inner = (Ellipse2D.Double) getShape()[0];
			Ellipse2D.Double next = new Ellipse2D.Double(getX() - (radius / 2) + getXVel(), getY() - (radius / 2) + getYVel(), radius, radius);
			if(inner.contains(new Point2D.Double(s.getCords().getX(), s.getCords().getY())) &&
					! next.contains(new Point2D.Double(s.getX() + s.getXVel(), s.getY() + s.getYVel()))) {
				//This works.		
				
				Point v = LinAlg.add(s.getVel(), LinAlg.scale(getVel(),-1));
				
				Point n = LinAlg.unitVectorBetween(cords, s.getCords());
				double temp = LinAlg.dotProduct(v, n);
				n = LinAlg.scale(n, -temp);
				s.acc(n);
				s.acc(n);
			}
		}
		if(s instanceof Shot) {
			Ellipse2D.Double center = new Ellipse2D.Double(getX(), getY(), 10, 10);
			if(LinAlg.intersect(s.getShape()[0], center)) {
				Game.game.registerAll(new Explosion(cords, getVel(), 5, 10));
				health -= ((Shot) s).getDamage();
				if(health < 0) {
					flag();
				}
				s.flag();
			}
		}

	}

	@Override
	public Shape[] getShape() {
		Shape[] s = { new Ellipse2D.Double(getX() - (radius / 2), getY() - (radius / 2), radius, radius)};
		return s;
	}

	@Override
	public double getRadius() {
		// TODO Auto-generated method stub
		return radius;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public double getMass() {
		return 50;
	}

}
